<?php

namespace app\admin\model;

use think\Model;

/**
 * Tavern
 */
class Tavern extends Model
{
    // 表名
    protected $name = 'tavern';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'city3_text',
    ];

    // 字段类型转换
    protected $type = [
        'array' => 'json',
    ];


    public function getArrayAttr($value): array
    {
        return !$value ? [] : json_decode($value, true);
    }

    public function getCity3Attr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCity3Attr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

//    public function getCity3TextAttr($value, $row): string
//    {
//        if ($row['city3'] === '' || $row['city3'] === null) return '';
//        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city3'])->column('name');
//        return $cityNames ? implode(',', $cityNames) : '';
//    }
}